package com.web;

import org.springframework.stereotype.Component;
import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * Tomcat启动时会触发WebApplicationInitializer的onStartup()方法。
 * 触发使用的机制是java spi， 在spring-web.jar下的META-INF中有配置ServletContainerInitializer
 * 其中配置了 org.springframework.web.SpringServletContainerInitializer
 * 会调用SpringServletContainerInitializer的onStartup()方法，
 *
 * SpringServletContainerInitializer.onStartup() 中调用 WebApplicationInitializer.onStartup
 */
@Component
public class MyWebApplicationInitializer extends MyServlet implements WebApplicationInitializer {

    /**
     * @param servletContext  是Tomcat上下文
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
         /*
         DispatcherServlet servlet = new DispatcherServlet(context);
         ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
         registration.setLoadOnStartup(1);
         registration.addMapping("/app/*");
         官网中上面这行代码就是下面这行
         */
        ServletRegistration.Dynamic registration = servletContext.addServlet("app",getServlet());
        registration.setLoadOnStartup(1);
        registration.addMapping("/app/*");

    }
}
